package com.yxr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxr.constants.SystemCanstants;
import com.yxr.domain.ResponseResult;
import com.yxr.domain.entity.SgArticle;
import com.yxr.domain.entity.SgCategory;
import com.yxr.domain.vo.CategoryVo;
import com.yxr.mapper.SgCategoryMapper;
import com.yxr.service.SgArticleService;
import com.yxr.service.SgCategoryService;

import com.yxr.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 分类表(SgCategory)表服务实现类
 *
 * @author makejava
 * @since 2024-07-05 20:41:34
 */
@Service("sgCategoryService")
public class SgCategoryServiceImpl extends ServiceImpl<SgCategoryMapper, SgCategory> implements SgCategoryService {

    @Autowired
    private SgArticleService articleService;

    @Override
    public ResponseResult getCategoryList() {
        //查询文章状态为已发布
        LambdaQueryWrapper<SgArticle> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SgArticle::getStatus, SystemCanstants.ARTICLE_STATUS_NORMAL);
        List<SgArticle> articles = articleService.list(queryWrapper);
        //获取文章的分类id
       Set<Long> categoryIDs =  articles.stream()
                .map(new Function<SgArticle, Long>() {
                    @Override
                    public Long apply(SgArticle sgArticle) {
                        return sgArticle.getCategoryId();
                    }
                }).collect(Collectors.toSet());
        //查询分类表
         List<SgCategory> categories = listByIds(categoryIDs);
        categories = categories.stream().filter(category ->SystemCanstants.STATUS_SIZE.equals(category.getStatus()) )
                 .collect(Collectors.toList());
        //vo
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanaList(categories, CategoryVo.class);
        return ResponseResult.okResult(categoryVos);


    }
}
